package jwlee.testgui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.text.DecimalFormat;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class UtmostClickAndMove extends JFrame {
	DecimalFormat df = new DecimalFormat("0.000");
	private long number;
	private long time = Calendar.getInstance().getTimeInMillis();
	private final long begintime = time;
	private double speed;
	
	private Point oldP = new Point(0,0);
	
	public UtmostClickAndMove() {
		init();
	}

	private void init() {
		this.setTitle("UtmostClick");
		this.setSize(600, 400);
		this.setLocation(200, 200);
		this.setResizable(false);

		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	private void testClickSpeed() {
		Container contentPane = this.getContentPane();

		JPanel speedP = new JPanel(new GridLayout(1, 3));
		final JLabel nowspeedL = new JLabel("now speed : ");
		final JLabel maxspeedL = new JLabel("max speed : ");
		final JLabel averagespeedL = new JLabel("average speed : ");
		speedP.add(nowspeedL);
		speedP.add(maxspeedL);
		speedP.add(averagespeedL);
		contentPane.add(speedP, BorderLayout.NORTH);

		final JButton clickB = new JButton("click here to test your speed...");
		clickB.setBackground(new Color(199, 237, 204));
		Font font = new Font("Times New Roman", Font.BOLD, 24);
		clickB.setFont(font);
		clickB.setBorder(BorderFactory.createRaisedBevelBorder());
		clickB.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getSource() == clickB) {
					number++;
					long newtime = Calendar.getInstance().getTimeInMillis();
					double newspeed = (double) 1000 / (newtime - time);
					double averagespeed = (double) number * 1000
							/ (newtime - begintime);
					time = newtime;
					nowspeedL.setText("now speed : " + df.format(newspeed));
					averagespeedL.setText("average speed : "
							+ df.format(averagespeed));
					if (newspeed > UtmostClickAndMove.this.speed) {
						UtmostClickAndMove.this.speed = newspeed;
						maxspeedL.setText("max speed : " + df.format(newspeed));
					}
				}
			}
		});
		contentPane.add(clickB, BorderLayout.CENTER);
	}
	
	private void testMoveSpeed() {
		Container contentPane = this.getContentPane();

		JPanel speedP = new JPanel(new GridLayout(1, 3));
		final JLabel nowspeedL = new JLabel("now speed : ");
		final JLabel maxspeedL = new JLabel("max speed : ");
		final JLabel averagespeedL = new JLabel("average speed : ");
		speedP.add(nowspeedL);
		speedP.add(maxspeedL);
		speedP.add(averagespeedL);
		contentPane.add(speedP, BorderLayout.NORTH);

		final JButton moveB = new JButton("move here to test your speed...");
		moveB.setBackground(new Color(199, 237, 204));
		Font font = new Font("Times New Roman", Font.BOLD, 24);
		moveB.setFont(font);
		moveB.setBorder(BorderFactory.createRaisedBevelBorder());
		moveB.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				Point newP = e.getPoint();
				long newtime = Calendar.getInstance().getTimeInMillis();
				double dis = Math.pow(Math.pow(newP.getX() - oldP.getX(), 2)
						+ Math.pow(newP.getY() - oldP.getY(), 2), 0.5);
				//像素换算成米，14.1寸屏，显示器对角
				double dis1 = dis*(14.1/30/Math.pow(Math.pow(1024, 2) + Math.pow(786, 2), 0.5));
				//毫秒换算成秒
				//System.out.println(dis1+"/"+(newtime-time));
				double moveSpeed = dis1/(newtime-time)*1000;
				nowspeedL.setText("now speed : " + df.format(moveSpeed));
				oldP = newP;
				time = newtime;
			}
		});
		contentPane.add(moveB, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		// getSystemFonts();
		UtmostClickAndMove tt = new UtmostClickAndMove();
		//tt.testClickSpeed();
		tt.testMoveSpeed();
	}

	private static void getSystemFonts() {
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		Font[] fonts = ge.getAllFonts();
		for (int i = 0; i < fonts.length; i++) {
			System.out.println(fonts[i].getName());
		}
	}
}
